
let express=require(`express`)
let router=express.Router()
let db=require(`../util/db`)
// 获取文章列表
router.get(`/my/article/cates`,(req,res)=>{
    db(`select * from category `,(err,result)=>{
        // console.log(err)
        // console.log(result)
        if(err||result.length===0) return res.send({code:1,msg:'sorry'})
        res.send({code:0,msg:"获取成功",data:result})
    })
})
// 新增文章分类
router.post(`/my/article/addcates`,(req,res)=>{
    let {name,alias}=req.body
    console.log(name,alias)
    db(`insert into category set name='${name}',alias='${alias}'`,(err,result)=>{
        // console.log(err)
        // console.log(result)
        if(err||result.affectedRows===0) return res.send({code:1,msg:'sorry'})
        res.send({code:0,msg:'新增成功'})
    })
})
// 根据id删除文章分类
router.get(`/my/article/deletecate/:id`,(req,res)=>{
    let{id}=req.params
    // console.log(req.params)
    console.log(id)
db(`delete from category where id=${id}`,(err,result)=>{
    console.log(err)
    console.log(result)
    if(err||result.affectedRows===0) return res.send({code:1,msg:'sorry'})
    res.send({code:0,msg:'删除成功'})
})
})
// 根据id获取文章分类数据
router.get(`/my/article/cates/:id`,(req,res)=>{
    let {id}=req.params
    console.log(id)
    db(`select * from category where id=${id}`,(err,result)=>{
        // console.log(err)
        // console.log(result)
        if(err||result.length===0) return res.send({cede:1,msg:'sorry'})
        res.send({code:0,msg:'获取分类成功',data:result[0]})
    })
})
// 根据id更新文章分类数据
router.post(`/my/article/updatecate`,(req,res)=>{
    let {id,name,alias}=req.body
    console.log(id)
    db(`update category set name='${name}',alias='${alias}' where id=${id}`,(err,result)=>{
        // console.log(err)
        // console.log(result)
        if(err||result.changedRows===0) return res.send({code:1,msg:'sorry'})
        res.send({code:0,msg:'更新成功'})
    })
})
module.exports=router
